#include "kuozhangbean.h"
#include "utils/citymanager.h"
#include "QJsonDocument"
#include "QJsonArray"
#include "tlbbcode.h"
#include "utils/runshopgoodsmanager.h"

KuoZhangBean::KuoZhangBean()
{

}

KuoZhangBean::KuoZhangBean(int num, QString groupCity, QString groupXiaoWeiLocation, int groupIndex, int taskTotal, QString shortCityName)
{
    this->num = num;
    this->groupCity = groupCity;
    this->groupXiaoWeiLocation = groupXiaoWeiLocation;
    this->groupIndex = groupIndex;
    this->taskTotal = taskTotal;
    this->shortCityName = shortCityName;
}

void KuoZhangBean::createObject(QJsonObject &obj, KuoZhangBean *kuoZhangBean)
{
    obj.insert("num", kuoZhangBean->num);
    obj.insert("groupCity", kuoZhangBean->groupCity);
    obj.insert("groupXiaoWeiLocation", kuoZhangBean->groupXiaoWeiLocation);
    obj.insert("groupIndex", kuoZhangBean->groupIndex);
    obj.insert("taskTotal", kuoZhangBean->taskTotal);
     obj.insert("shortCityName", kuoZhangBean->shortCityName);
}


QList<KuoZhangBean> KuoZhangBean::jsonToArray(QByteArray jsonArrayStr)
{
    QJsonParseError jsonError;
    QList<KuoZhangBean> kuoZhangBeans;
    const QJsonDocument &document = QJsonDocument::fromJson(jsonArrayStr, &jsonError);
    if(document.isNull() || jsonError.error != QJsonParseError::NoError) return kuoZhangBeans;
    QJsonArray jsonArray = document.array(); //文档转化为Json对象
    for(int i = 0 ; i < jsonArray.size() ; i++){
        QJsonObject obj = jsonArray.at(i).toObject();

        int num = obj.value("num").toInt();
        QString groupCity = obj.value("groupCity").toString();
        QString groupXiaoWeiLocation = obj.value("groupXiaoWeiLocationX").toString("");
        int groupIndex = obj.value("groupIndex").toInt();
        int taskTotal = obj.value("taskTotal").toInt();
        QString shortCityName = obj.value("shortCityName").toString();
        kuoZhangBeans.append(KuoZhangBean(num, groupCity, groupXiaoWeiLocation, groupIndex, taskTotal, shortCityName));
    }
    return kuoZhangBeans;
}
QByteArray KuoZhangBean::ArraytoJson(QList<KuoZhangBean*> *kuoZhangBeans)
{
    QJsonDocument doc;
    QJsonArray array;
    if(kuoZhangBeans->isEmpty()) return "";
    for(int i = 0; i< kuoZhangBeans->size(); i++){
        QJsonObject obj;
        KuoZhangBean *kuoZhangBean = kuoZhangBeans->at(i);
        createObject(obj, kuoZhangBean);
        array.append(obj);
    }
    doc.setArray(array);
    return doc.toJson();
}


void KuoZhangBean::setLine()
{
    CityNode *groupCityNode = CityManager::getInstance()->getCityNodeByName(groupCity);
    if(groupCityNode == nullptr){
        return;
    }
    groupCityEn = groupCityNode->enName;


     CityNode *shortCityNode = CityManager::getInstance()->getCityNodeByName(shortCityName);
     if(shortCityNode == nullptr){
         return;
     }

    if(!CityManager::getInstance()->getRunShop(shortCityNode->enName, groupCityEn, &backLine, nullptr)){
        return;
    }
    CityNode *lastCityNode = backLine.last();
    CityXiaoWeiBean* xiaoweiBean = CityManager::getInstance()->getXiaoWeiBean(lastCityNode, groupXiaoWeiLocation);
    if(xiaoweiBean == nullptr) return;
    CityNode *xiaowei = new CityNode(groupXiaoWeiLocation, XIAO_WEI, RunShopGoodsManager::getInstance()->getXiaoWeiLocationsImgs(groupXiaoWeiLocation),xiaoweiBean->x, xiaoweiBean->y);
    backLine.append(xiaowei);
}
